import * as commonService from '../services/CommonCRUD';
import * as datetime from '../utils/datetime'
import { message } from 'antd';
import * as common from '../utils/common'

export default {
    namespace: null,
    state: {
        list: [],
        optionDatas: {},
        keyName: "ID",
        reload: false
    },
    reducers: {
        setListData(state, { payload: { data: list, keyName} }) {
            return { ...state, list, keyName };
        },
        setOptions(state, { payload: { optionDatas } }) {
            return { ...state, optionDatas };
        },
        resetListData(state, { payload: { data: list, keys } }) {
            let newIndex = 0
            const keyName = state.keyName
            list.map(d => {
                if (d.$state === 1) {
                    d[keyName] = keys[newIndex][keyName]
                    newIndex++
                }
                d.$state = 0
            })
            return { ...state, list, reload: true };
        },
    },
    effects: {
        *fetchAll({ payload: { daoName, keyName } }, { call, put }) {
            try {
                const listData = yield call(commonService.fetchAll, { daoName });
                if (listData) {
                    yield put({
                        type: 'setListData',
                        payload: {
                            data: listData,
                            keyName
                        },
                    });
                }
            } catch (e) {
                console.log(e)
            }
        },
        *fetchOptions({payload: { options } }, { call, put }) {
            let optionDatas = {}
            for (let i = 0; i < options.length; i++) {
                const o = options[i];
                const data = yield call(commonService.fetchOption, { daoName: o.daoName });
                if (data) {
                    let pData = {}
                    data.map((l, i) => {
                        let pNode = l[o.parentTpl]
                        if (!pNode) {
                            pNode = "top-node"
                        }
                        if (!pData[pNode]) {
                            pData[pNode] = []
                        }
                        pData[pNode].push(i)
                    })
                    let cascaderKeys = {}
                    const createTreeData = (t, p, keys) => {
                        if (!pData[p]) {
                            return
                        }
                        pData[p].map(i => {
                            const id = data[i][o.valueTpl];
                            const strId = '' + id;
                            const node = { value: strId, label: data[i][o.optionTpl], key: id }
                            let thisKeys = []
                            if (keys) {
                                thisKeys = keys.concat();
                            }
                            thisKeys.push(strId);
                            if (pData[id]) {
                                node.children = []
                                createTreeData(node.children, id, thisKeys)
                            }
                            cascaderKeys[id] = thisKeys
                            t.push(node)
                        })
                        return t;
                    }
                    let treeData = createTreeData([], "top-node");
                    optionDatas[o.daoName] = treeData;
                    if (o.isCascader) {
                        optionDatas[o.daoName + "_cascader"] = cascaderKeys;
                    }
                }
            }
            yield put({
                type: 'setOptions',
                payload: { optionDatas }
            });
        },
        *submitData({ payload: {daoName, datas, delDatas }}, { call, put, select }) {
            const {keyName} = yield select(state => state[daoName]);
            let newDatas = [], updateDatas = [], deleteDatas = delDatas
            datas.map(d => {
                if (d.$state === 1) {
                    d[keyName] = null
                    newDatas.push(d)
                } else if (d.$state === 2) {
                    updateDatas.push(d)
                }
            })
            const keys = yield call(commonService.submitDatas, { daoName, newDatas, updateDatas, deleteDatas });
            yield put({ type: 'resetListData', payload: { data: datas, keys } });
            message.success("数据提交成功！", 5)
        },
    },
};
